from fastapi import APIRouter
from fastapi import Request
from util.MyExceptionHandler import MyExceptionHandler
import inspect
from util import Logger
from service.SysDictService import SysDictService
from service.UserService import UserService
from service.PatientService import PatientService
from service.RecordService import RecordService
from service.DeptService import DeptService
from StatusCode import StatusCode

router = APIRouter(tags=["系统管理路由"])


@router.get("/system/init")
def initSystem(request: Request):
    response = {"code": 200}
    try:
        SysDictService().initDB()
        UserService().initDB()
        DeptService().initDB()
        PatientService().initDB()
        RecordService().initDB()
        response["msg"] = "init system success"
    except Exception as e:
        Logger.logger(e)
        response["msg"] = "fail"
    return response


@router.get("/system/dict")
def getSysDict(request: Request):
    try:
        json_data = dict(request.query_params)
        key = json_data["key"]
        df = SysDictService().getDict(key)
        response = {"msg": "success", "code": 200, "data": df.to_dict(orient="records")}
    except Exception as e:
        Logger.logger(e)
        response = {"msg": "success", "code": 200, "data": []}
    return response
